package ee.rawre.tafayc.util;

import java.util.Iterator;
import java.util.Map;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ResultChart extends HTML {
   
   private CheckBox characters;
   private Map<String, int[]> cookieMap;

   public ResultChart(CookieManager cookieManager) {
      super();
      try {
         cookieMap = cookieManager.getCookieMap();
         Iterator<String> iterator = cookieMap.keySet().iterator();
         FlowPanel flow = new FlowPanel();
         HorizontalPanel menuBar = new HorizontalPanel();
         menuBar.add(characters = new CheckBox("characters"));
         characters.addValueChangeHandler(new CheckBoxValueChangeHandler());
         characters.setValue(true, true);
         flow.add(menuBar);
         while (iterator.hasNext()) {
            String key = iterator.next().toString();  
            int[] value = cookieMap.get(key);  
            flow.add(drawResult("cpm", value[2]));
            flow.add(drawResult("wpm", value[3]));
            System.out.println(key);
         } 
         setHTML(flow.getElement().getInnerHTML());
      } catch (Exception e) {
         setHTML("Nothing found.");
      }
   }
   

   

   
   private Widget drawResult(String name, int height) {
      HTML html = new HTML();
      html.setStyleName(name + " result");
      html.setTitle(Integer.toString(height));
      html.setHeight(height/4 + "px");
      html.setWidth("30px");
      if (!name.equals("cpm")) html.addStyleName("mlminus"); else html.addStyleName("ml5px");
      return html;
   }
   
   @Override
   public String getHTML() {
      // TODO Auto-generated method stub
      return super.getHTML();
   }

   @Override
   public void setHTML(String html) {
      // TODO Auto-generated method stub
      super.setHTML(html);
   }
   
//   private int findLargest(Map<String, int[]> cookieMap, int index) {
//      
//      return 0;
//   }

   private class CheckBoxValueChangeHandler implements ValueChangeHandler<Boolean> {

      @Override
      public void onValueChange(ValueChangeEvent<Boolean> event) {
         System.out.println("Yay");

      }

   }
}
